package com.fmi.timedealer.reporting;

import java.util.ArrayList;
import java.util.List;

import com.fmi.timedealer.R;
import com.fmi.timedealer.datamanager.IDataManager;
import com.fmi.timedealer.datamanager.SQLiteDataManager;
import com.fmi.timedealer.reporting.model.ReportOptions;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ReportsActivity extends ListActivity {
	
	// data to pass to current report activity being called by this activity
	public static final String SELECTED_CATEGORY = "SelectedCatgory";
	public static final String SELECTED_PERIOD = "SelectedPeriod";
	
	// constants for reprot time perod choice
	public static final String TIME_PERIOD_DAY = "Last Week";
	public static final String TIME_PERIOD_MONTH = "Last Month";
	public static final String TIME_PERIOD_ALL = "All";
	
	private ReportOptionsAdapter adapter;
	
	private List<ReportOptions> reportOptions = new ArrayList<ReportOptions>();
	
	private IDataManager dbManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.create_report);
        
        for(ReportOptions option : ReportOptions.values()) {
			reportOptions.add(option);
		}
        
		dbManager = new SQLiteDataManager("MyDB", this);

        adapter = new ReportOptionsAdapter(this, R.layout.report_option_item);
        setListAdapter(adapter);
        
        int[] colors = { 0, Color.GRAY, 0 };
		getListView().setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
		getListView().setDividerHeight(2);
		
		getListView().setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ReportOptions selectedOption = reportOptions.get(position);
				switch(selectedOption.getCode()) {
				case(0):
					showCategoiriesTimePeriodOptions();
					break;
				case(1):
					showCategoriesOptions();
					break;
				case(2):
					showMoodTimePeriodOptions();
					break;
				default:
					createCustomReportActivity();
				}
			}
		});
    }
    
    private void createCustomReportActivity() {
    	Intent i = new Intent(ReportsActivity.this, CustomReportActivity.class);
		startActivity(i);
	}

	private void showMoodTimePeriodOptions() {
		
		final String[] timePeriodOptions = new String[] {TIME_PERIOD_DAY, TIME_PERIOD_MONTH, TIME_PERIOD_ALL};
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("Choose report time period");
		builder.setItems(timePeriodOptions, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				Intent i = new Intent(ReportsActivity.this, MoodsReportActivity.class);
				i.putExtra(ReportsActivity.SELECTED_PERIOD, timePeriodOptions[item]);
				startActivity(i);
			}
		});
    	
    	AlertDialog timePeriodOptionsDialog = builder.create();
    	timePeriodOptionsDialog.show();
	}

	private void showCategoriesOptions() {
		
		final String[] categories = dbManager.getCategoriesNames();
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("Choose category");
    	builder.setItems(categories, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				String selectedCategory = categories[item];
				Intent i = new Intent(ReportsActivity.this, ActivitiesPerCategoryReportActivity.class);
				i.putExtra(ReportsActivity.SELECTED_CATEGORY, selectedCategory);
				startActivity(i);
			}
		});
    	
    	AlertDialog timePeriodOptionsDialog = builder.create();
    	timePeriodOptionsDialog.show();
	}

	private void showCategoiriesTimePeriodOptions() {
		
		final String[] timePeriodOptions = new String[] {TIME_PERIOD_DAY, TIME_PERIOD_MONTH, TIME_PERIOD_ALL};
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("Choose report time period");
		builder.setItems(timePeriodOptions, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				Intent i = new Intent(ReportsActivity.this, CategoriesReportActivity.class);
				i.putExtra(ReportsActivity.SELECTED_PERIOD, timePeriodOptions[item]);
				startActivity(i);
			}
		});
    	
    	AlertDialog timePeriodOptionsDialog = builder.create();
    	timePeriodOptionsDialog.show();
	}
    
    private class ReportOptionsAdapter extends ArrayAdapter<ReportOptions> {
    	    
		public ReportOptionsAdapter(Context context, int textViewRecourceId) {
			
			super(context, textViewRecourceId);
			for(ReportOptions option : reportOptions) {
				add(option);
			}
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			ReportOptions currentOption = reportOptions.get(position);

			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v =  vi.inflate(R.layout.report_option_item, null);
			}
			
			TextView labelView = (TextView) v.findViewById(R.id.report_option_label);
			labelView.setText(currentOption.getLabel());
			
			ImageView iconView = (ImageView) v.findViewById(R.id.report_option_icon);
			iconView.setImageResource(currentOption.getIcon());

			return v;
		}
	}
}
